package com.shop2cn.iapi.agentproducts.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.GetCatalogRootIdBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.service.GetCatalogRootIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.AgentUsers;

/**
 * 查询规格货头idTest FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetCatalogRootId {
	private static GetCatalogRootIdBean getcatalogrootidBean;
	private static GetCatalogRootIdCall getcatalogrootidCall;

	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";

	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询规格货头idTest");
	}

	@Before
	public void caseUp() {
		getcatalogrootidBean = new GetCatalogRootIdBean();
		getcatalogrootidCall = new GetCatalogRootIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcatalogrootidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCatalogRootId_001() {
		Logger.start(true, "代理查询上级规格id，成功");
		try {
			
			//vip渠道
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//vip代理1
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			
			List<YmtCatalogsI> ymtCatalogsIList = ymtCatalogsIWapper.selectCatalogsByProductId(agentProductId);
			List<String> catalogIdList = new ArrayList<>();
			ymtCatalogsIList.forEach(p -> {
				catalogIdList.add(p.getsCatalogId());
			});
			
			List<YmtCatalogsI> ymtCatalogsIList1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			List<String> catalogIdList1 = new ArrayList<>();
			ymtCatalogsIList1.forEach(p -> {
				catalogIdList1.add(p.getsCatalogId());
			});

			getcatalogrootidBean.setCatalogIds(catalogIdList);
			getcatalogrootidCall.setData(getcatalogrootidBean);
			getcatalogrootidCall.callService();
			Logger.verifyEquals("200", getcatalogrootidCall.getString("code"), "验证code");
			
			JsonArray catalogs = getcatalogrootidCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogs");
			for (int i = 0; i < catalogs.size(); i++) {
				JsonObject catalog = catalogs.get(i).getAsJsonObject();
				if(catalogIdList.get(i).equals(catalog.get("catalogId").getAsString())){
					Logger.verifyEquals(catalog.get("rootCatalogId").getAsString(),catalogIdList1.get(i),"验证货头规格id");
					Logger.verifyEquals(catalog.get("rootProductId").getAsString(),productId,"验证货头商品id");
					Logger.verifyEquals(catalog.get("rootUserId").getAsInt(),vipRootUserId,"验证货头用户id");
				}
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}